<html>
<head><meta charset="utf-8"><title>does compiler optimize macros codegen different? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html">does compiler optimize macros codegen different?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="207935928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207935928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207935928">(Aug 25 2020 at 08:24)</a>:</h4>
<p><a href="https://rust.godbolt.org/z/Wxe8qv">https://rust.godbolt.org/z/Wxe8qv</a></p>
<p>I want to change <code>format!</code> so it expand like in <code>foo</code> <a href="https://github.com/rust-lang/rust/issues/75894">#75894</a> .<br>
However, <code>bar</code> is not optimized to a <code>ret</code> inst just like <code>foo</code> and <code>baz</code>.<br>
Does macros affect how code is optimized?</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="w">            </span><span class="k">match</span><span class="w"> </span>::<span class="n">std</span>::<span class="n">fmt</span>::<span class="n">Arguments</span>::<span class="n">new_v1</span><span class="p">(</span><span class="o">&amp;</span><span class="p">[</span><span class="s">&quot;asdfasdfasdfasfasfasd&quot;</span><span class="p">],</span><span class="w"></span>
<span class="w">                                                 </span><span class="o">&amp;</span><span class="k">match</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">[],</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">args</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">args</span><span class="p">.</span><span class="n">as_str</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="o">&lt;</span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span>::<span class="n">std</span>::<span class="n">borrow</span>::<span class="nb">ToOwned</span><span class="o">&gt;</span>::<span class="n">to_owned</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"></span>
<span class="w">                    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>::<span class="n">std</span>::<span class="n">fmt</span>::<span class="n">format</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">};</span><span class="w"></span>
<span class="w">        </span><span class="n">r</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">bar</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">format</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;a long story&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">baz</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">String</span>::<span class="n">from</span><span class="p">(</span><span class="s">&quot;a long story&quot;</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="207936724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207936724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207936724">(Aug 25 2020 at 08:35)</a>:</h4>
<p>Macros are expanded way before any optinization passes.</p>



<a name="207937004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207937004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207937004">(Aug 25 2020 at 08:39)</a>:</h4>
<p>It might make sense to look at the pre optimization mir and look for differences</p>



<a name="207938038"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207938038" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207938038">(Aug 25 2020 at 08:52)</a>:</h4>
<p>How can I do that ?</p>



<a name="207938168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207938168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207938168">(Aug 25 2020 at 08:54)</a>:</h4>
<p><a href="https://rust.godbolt.org/z/h9j76e">https://rust.godbolt.org/z/h9j76e</a></p>



<a name="207938186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207938186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207938186">(Aug 25 2020 at 08:54)</a>:</h4>
<p>you may have to smack a <code>-Zmir-opt-level=0</code> on top for this to be helpful</p>



<a name="207938230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207938230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207938230">(Aug 25 2020 at 08:55)</a>:</h4>
<p>iirc there is a way to instead just return the mir before the from before the first optimization pass, but I don't remember the name rn</p>



<a name="207940200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207940200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207940200">(Aug 25 2020 at 09:20)</a>:</h4>
<p>Is stage 1 compiler and stage 2 different in optimization result ?</p>



<a name="207940313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207940313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207940313">(Aug 25 2020 at 09:22)</a>:</h4>
<p>stage 1 and 2 should result in the same output</p>



<a name="207940375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207940375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207940375">(Aug 25 2020 at 09:23)</a>:</h4>
<p>they are the same program, "just" compiled with a different compiler</p>



<a name="207977885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/does%20compiler%20optimize%20macros%20codegen%20different%3F/near/207977885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/does.20compiler.20optimize.20macros.20codegen.20different.3F.html#207977885">(Aug 25 2020 at 15:27)</a>:</h4>
<p>turned out that my system llvm (v10.0.1) doesn't optimize the code like the one rustc uses,<br>
codegen tests should be passed now.<br>
Thanks for helping me!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>